﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
{
    class Booking
    {
        //Connection connetionObj;
        IList<Reservation> list;
        public Booking() { 

           // this.connetionObj = connection;
            list = new List<Reservation>();
                         
        }
        

        //checks if there is enough seats left on all flights in connection
        private bool Register(Connection connection, int seatsWanted) {
            bool success = false;
           foreach (Flight item in connection.FlightList())
	        {
                if ((item.noOfSeats - seatsWanted) >= 0) //if there are less seats than wanted, abort booking.
                {
                    if ((item.noOfSeats - seatsWanted) == 0)
                    {
                        //remove flight from list but continue this booking and kill all other running bookings that involves this flight
                    }
                     item.noOfSeats = item.noOfSeats - seatsWanted; //to decrease number of available seats
                    // for(int i = 0; i < seatsWanted; i++)
                   // { 
                            // seatNO = seatsAvailable + i; or something like that to specifie seats numbers if needed, I don't enable this because not sure if needed
                   // }
                     string fname = "Name";
                        string lname = "Lastname";
                   string address = "address";
                  string  country = "country";
                   string phone = "phone";
                   string email = "email";
                   string passport = "passport";
                    list.Add(new Reservation(item, seatsWanted, fname, lname, address, country, phone, email, passport)); // all basic info comes from GUI so for now we'll use presets
                    success = true;
                }
                else { 
                //there is not enough seats left
                    //inform customers ?
                        //abort booking
                    cancelReservations();
                    success = false;
                }
	        } 
            return success;
        }

        private void cancelReservations() { 
        
            list.Clear();
        
        }
        //books tickets
        public IList<Reservation> book(Connection connection, int seatsWanted)
        {
            if (Register(connection, seatsWanted)) //performs booking and if reservation list if filled correctly gives true, othervise aborts booking
            {
                try
                {
                   // writeToDatabase(reservationObj);
                    

                }
                catch (Exception e) { //catches exception if there is one, (like null pointer from reservationObj)
                    Console.WriteLine(e.StackTrace);
                }
            }

            return list;

        }
    }
}
